package com.dmlll.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dmlll.common.ResultObj;
import com.dmlll.domain.HospitalClose;
import com.dmlll.domain.HospitalCloseDetail;
import com.dmlll.domain.HospitalPurchaseInput;
import com.dmlll.service.HospitalCloseDetailService;
import com.dmlll.service.HospitalCloseService;
import com.dmlll.service.HospitalPurchaseInputService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dmLll
 * @since 2021-03-31
 */
@RestController
public class HospitalCloseDetailController {
    @Autowired
    private HospitalCloseDetailService hospitalCloseDetailService;
    @Autowired
    private HospitalCloseService hospitalCloseService;
    @Autowired
    private HospitalPurchaseInputService hospitalPurchaseInputService;
    /**
     * 批量添加结算详情
     * @param drugMessageIds
     * @param purchaseIds
     * @param closeId
     * @return
     */
    @RequestMapping("addHospitalCloseDetail")
    public ResultObj addHospitalCloseDetail(Integer[] drugMessageIds,Integer[] purchaseIds,Integer closeId){
        List<HospitalCloseDetail> list = new ArrayList<>();
        for (int i = 0;i < drugMessageIds.length;i++) {
            HospitalCloseDetail hcd = new HospitalCloseDetail();
            hcd.setCloseId(closeId);
            hcd.setPurchaseId(purchaseIds[i]);
            hcd.setDrugMessageId(drugMessageIds[i]);
            hcd.setCloseState(1);//未确认结算
            //计算结算数量，结算金额(入库数量、入库金额)
            QueryWrapper<HospitalPurchaseInput> wrapper = new QueryWrapper<>();
            wrapper.eq("drug_message_id",hcd.getDrugMessageId());
            wrapper.eq("purchase_id",hcd.getPurchaseId());
            HospitalPurchaseInput purchaseInput = hospitalPurchaseInputService.getOne(wrapper);
            hcd.setCloseNum(purchaseInput.getInputNum());
            hcd.setClosePrice(purchaseInput.getInputPrice());
            list.add(hcd);
        }
        //添加
        try {
            hospitalCloseDetailService.saveBatch(list);
        } catch (Exception e) {
            return ResultObj.ADD_ERROR;
        }
        return ResultObj.ADD_SUCCESS;
    }

    /**
     * 批量删除结算详情
     * @param closeDrupDetailIds
     * @return
     */
    @RequestMapping("deleteCloseDrupDetail")
    public ResultObj deleteCloseDrupDetail(Integer[] closeDrupDetailIds){
        try {
            hospitalCloseDetailService.removeByIds(Arrays.asList(closeDrupDetailIds));
        } catch (Exception e) {
            return ResultObj.DELETE_ERROR;
        }
        return ResultObj.DELETE_SUCCESS;
    }
}

